"use strict";
/* Controllers */
app.controller("ticketAddController", ['$scope', '$location', '$rootScope', '$q', '$routeParams', 'ticketAddService', function ($scope, $location, $rootScope, $q, $routeParams, ticketAddService) {

    var vm = $scope.vm = {};
    vm.p = vm.d = {};
    vm.p.id = $routeParams.id; // 如果id!=0为更新
    vm.d.assignType = 'person';
//    vm.operate_type = $routeParams.operate_type; // 如果id!=0为更新
    vm.isDisabled = false;
//    if ($routeParams.operate_type == 'deal') {
//        vm.isDisabled = true
//    } else {
//        vm.isDisabled = false
//    }
    $scope.checkAssign = function () {
        var vm = $scope.vm;
        if(vm.d.assignType == 'person') {
            return (vm.p.responser!=null)?true:false;
        }else {
            return ((vm.p.resolvegroupId!=null)&&(vm.p.ctiId!=null))?true:false;
        }
        return false;
    }

    var errorFn = function (result) {
        alert(result);
    };


    $scope.listResolveGroup = function () {
        var timestamp1 = Date.parse(new Date());
        console.log(timestamp1 + "listResolveGroup")
        var vm = $scope.vm;
        var params = {};
        var procFn = function (res) {
            if (res.success && res.data) {
                res.data.forEach(function (item) {
                    if (item.ctiIds != null && typeof (item.ctiIds) != 'undefined') {
                        var ctiIdsArr = item.ctiIds.split(',')
                        var finalCtiIds = ''
                        ctiIdsArr.forEach(function (ctiId) {
                            finalCtiIds = finalCtiIds + ctiId.split('|')[1] + ','
                        })
                        finalCtiIds = finalCtiIds.substring(0, finalCtiIds.length - 1);
                        item.ctiIds = finalCtiIds
                    }
                })
                vm.listResolveGroup = res.data;
                $scope.rgMaps = {}
                for (var index in vm.listResolveGroup) {
                    $scope.rgMaps[vm.listResolveGroup[index].resolvegroupId] = vm.listResolveGroup[index]
                }
                console.log("rgMaps" + $scope.rgMaps)
            } else {
                alert(res.msg);
            }
        };
        return ticketAddService.listResolveGroup(params, procFn, errorFn);
    };


    $scope.listCti = function () {
        var timestamp1 = Date.parse(new Date());
        console.log(timestamp1 + "listCti")
        var vm = $scope.vm;
        var params = {};
        var procFn = function (res) {
            if (res.success && res.data) {
                vm.listCti = res.data;
                $scope.ctiMaps = {}
                for (var index in $scope.vm.listCti) {
                    $scope.ctiMaps[$scope.vm.listCti[index].ctiId] = $scope.vm.listCti[index]
                }
                console.log("ctiMaps" + $scope.ctiMaps)
            } else {
                alert(res.msg);
            }
        };
        return ticketAddService.listCti(params, procFn, errorFn);
    };

    $scope.listUser = function () {
        var vm = $scope.vm;
        var params = {};
        var procFn = function (res) {
            if (res.success && res.data) {
                vm.listUser = res.data;
            } else {
                alert(res.msg);
            }
        };
        return ticketAddService.listUser(params, procFn, errorFn);
    };

    $scope.pingDetail = function () {
        var vm = $scope.vm;
        var params = {};
        var procFn = function (response) {
            var data = response.data;
            if (data == null) return;
            vm.p.requester = data[0].userName
        };
        return ticketAddService.pingDetail(params, procFn, errorFn);
    };


    $scope.getCtiById = function () {
        var vm = $scope.vm;
        var timestamp1 = Date.parse(new Date());
        console.log(timestamp1 + "getCtiById")
        console.log('getCtiById' + $scope.rgMaps)
        console.log('getCtiById' + $scope.ctiMaps)

        if(vm.p.resolvegroupId==null || vm.p.resolvegroupId=='') {
            return;
        }
        var ctiIds = $scope.rgMaps[vm.p.resolvegroupId].ctiIds.split(",")
        $scope.ctis = {}
        for (var index in ctiIds) {
            $scope.ctis[ctiIds[index]] = $scope.ctiMaps[ctiIds[index]]
        }
    }
    $scope.addTicket = function () {
        var vm = $scope.vm;
        if(! $scope.checkAssign()) {
            alert("指派人或者支持组信息不完整，请设置。");
            return;
        }
        var params = vm.p;
        var procFn = function (response) {
            if (response.success && response.data) {
                alert("执行成功！")
            } else {
                alert(response.msg);
            }
        };
        return ticketAddService.addTicket(params, procFn, errorFn);
    };

    $scope.getTicket = function () {
        var vm = $scope.vm;
        var params = {
            id: vm.p.id
        };
        var procFn = function (res) {
            if (res.success && res.data) {
                vm.p = res.data;
                if(vm.p.resolvegroupId!=null && vm.p.resolvegroupId.length>1) {
                    vm.d.assignType = 'resolvegroup';
                }
            } else {
                alert(res.msg);
            }
        };
        return ticketAddService.getTicket(params, procFn, errorFn);
    };


    $scope.repeatFinish = function () {
        $scope.$apply(function() {
        });
        var vm = $scope.vm;
        if (vm.p.responser != null && typeof(vm.p.responser) != 'undefined') {
            $('.selectpicker').selectpicker('val', vm.p.responser);
        } else {
            $('.selectpicker').selectpicker('val', '')
        }
    };
    $scope.initializeController = function() {
        if (vm.p.id != 0) {
            console.log(vm.p.id);
            //        $scope.getTicket().then(function(){
            //            return $scope.listResolveGroup()
            //        }).then(function(){
            //            return $scope.listCti()
            //        }).then(function(){
            //            return $scope.getCtiById()
            //        })
            $scope.listUser()
            $q.all([$scope.getTicket(), $scope.listResolveGroup(), $scope.listCti()]).then(function () {
                $scope.pingDetail()
                $scope.getCtiById()
            })
        } else {
            $q.all([$scope.pingDetail()]).then(function () {
                $scope.listResolveGroup()
                $scope.listCti()
                $scope.listUser()
            })
        }
    }
    $scope.initializeController();

}]);


/* Services */
app.service('ticketAddService', ['ajaxService', function (ajaxService) {

    this.addTicket = function (tickets, successFunction, errorFunction) {
        return ajaxService.AjaxPost(tickets, "/star/api/onduty-app-service/v1/ticket/addTicket.do", successFunction, errorFunction);
    };

    this.getTicket = function (tickets, successFunction, errorFunction) {
        return ajaxService.AjaxPost(tickets, "/star/api/onduty-app-service/v1/ticket/getTicket.do", successFunction, errorFunction);
    };

    this.listResolveGroup = function (params, successFunction, errorFunction) {
        return ajaxService.AjaxPost(params, "/star/api/onduty-app-service/v1/resolvegroup/listResolvegroup.do", successFunction, errorFunction);
    };

    this.listCti = function (params, successFunction, errorFunction) {
        return ajaxService.AjaxPost(params, "/star/api/onduty-app-service/v1/cti/listCti.do", successFunction, errorFunction);
    };

    this.listUser = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/user/user_list.do", successFunction, errorFunction);
    };

    this.pingDetail = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/aaa/pingDetail.do", successFunction, errorFunction);
    };

}]);

//自定义指令实现ng-repeat完成后执行repeatFinish方法
app.directive('myRepeatFinish', function () {
    return {
        restrict: 'A',
        link: function (scope, element, attr) {
//            console.log(scope.$index);
            if (scope.$last == true) {
                console.log('ng-repeat over, will draw chart.');
                scope.$eval(attr.myRepeatFinish)
            }
        }
    }
});
